package it.traveldreamWeb.authentification;

import it.traveldream.beans.BookingManagerInterface;
import it.traveldream.dto.HotelDTO.HotelRoomType;
import it.traveldream.dto.ReservationDTO;
import it.traveldream.exceptions.ReservationNotFoundException;
import it.traveldream.utils.CalendarUtils;
import it.traveldreamWeb.customer.ReservationBean;
import it.traveldreamWeb.shared.HotelRoom;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class PublicBean {

	private String invitationLink;
	@EJB
	private BookingManagerInterface bookingManager;
	
	private BigDecimal currentPrice;
	private ReservationDTO reservation;
	private List<HotelRoom> rooms;
	private String error;
	private String message;
	
	@ManagedProperty(value="#{reservationBean}")
	private ReservationBean session;
	
	public void clean(){
		this.currentPrice = null;
		this.reservation = null;
		this.setRooms(null);
	}
	
	public void checkLink(){
		try{
			ReservationDTO r = this.bookingManager.getReservationByLink(this.invitationLink);
			if (r == null)
				throw new ReservationNotFoundException();
			
			Calendar l = Calendar.getInstance();
 	        Calendar e = Calendar.getInstance();
 	        l.setTime(r.getLeavingDate());
 	        e.setTime(r.getEndDate());
 	        int duration = (int) CalendarUtils.daysBetween(l, e); 
				
			this.setCurrentPrice(this.bookingManager.getTotalPrice(r.getMean(), 1, duration, 
					r.getHotel(), r.getHotelRooms(), r.getExcursions(), false));
			
			this.reservation = new ReservationDTO();
			
			this.reservation.setCustomer(r.getCustomer());
			this.reservation.setEndDate(r.getEndDate());
			this.reservation.setExcursions(r.getExcursions());
			this.reservation.setHotel(r.getHotel());
			this.reservation.setHotelRooms(r.getHotelRooms());
			this.rooms = new ArrayList<>();
			for (HotelRoomType type : r.getHotelRooms().keySet()){
				HotelRoom h = new HotelRoom();
				h.setType(type);
				h.setTotalRooms(r.getHotel().getRooms().get(type));
				h.setPrice(r.getHotel().getPrices().get(type));
				h.setSelectedRooms(0);
				this.rooms.add(h);
			}
			
			this.reservation.setLeavingDate(r.getLeavingDate());
			this.reservation.setMean(r.getMean());
			this.reservation.setTravellers(1);
		} catch (ReservationNotFoundException e){
			this.setError("Reservation not found. Wrong invitation link?");
		} catch (Exception e){
			this.setError("Internal error. Please start over");
		}
	}
	
	public String join(){
		if (this.reservation == null){
			this.checkLink();
		}
		boolean someRoom = false;
		for (HotelRoom h : this.rooms)
			if (h.getSelectedRooms() > 0){
				someRoom = true;
				break;
			}
		if (! someRoom){
			this.setMessage("Please, choose some room first");
			return null;
		}
		
		if (this.session.getCustomer() == null)
			return "Registration?faces-redirect=true";
		
		// gia loggato
		return "Customer/home?faces-redirect=true";
	}
	
	public String getInvitationLink() {
		return this.invitationLink;
	}


	public void setInvitationLink(String invitationLink) {
		this.invitationLink = invitationLink;
	}


	public ReservationDTO getReservation() {
		return this.reservation;
	}


	public void setReservation(ReservationDTO reservation) {
		this.reservation = reservation;
	}
	
	public BigDecimal getCurrentPrice() {
		return this.currentPrice;
	}

	public void setCurrentPrice(BigDecimal currentPrice) {
		this.currentPrice = currentPrice;
	}

	public void setSession(ReservationBean session) {
		this.session = session;
	}

	public String getError() {
		return this.error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public List<HotelRoom> getRooms() {
		return this.rooms;
	}

	public void setRooms(List<HotelRoom> rooms) {
		this.rooms = rooms;
	}

	public String getMessage() {
		return this.message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}
